package com.chouren.goandroid.ui.listview.indexable;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class LetterView extends View {

	private static final String LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	public int colorTouched = 0x22000000;
	public int colorUntouched = 0x00000000;
	public int colorSelected = 0xff386AB7;
	public int colorUnSelected = 0x5e000000;
	public int textSize = 16;
	private Paint paint;
	private float letterHeight;
	private int currentIndex;
	private OnLetterChangeListener letterChangeListener;

	public LetterView(Context context){
		this(context, null);
	}
	
	public LetterView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	
	private void init() {
		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setTextSize(textSize);
		paint.setFakeBoldText(true);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		letterHeight = getHeight() / LETTERS.length();
		
		for(int i = 0; i < LETTERS.length(); i++) {
			if(i == currentIndex) {
				paint.setTextSize(22);
				paint.setColor(colorSelected);
			}else {
				paint.setTextSize(textSize);
				paint.setColor(colorUnSelected);
			}
			canvas.drawText(String.valueOf(LETTERS.charAt(i)), (getWidth() - textSize)/2, letterHeight * (i + 1), paint);
		}
	}
	
	@SuppressLint("ClickableViewAccessibility")
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		currentIndex = (int)event.getY() / (int)letterHeight;
		
		if(currentIndex < 0) currentIndex = 0;
		if(currentIndex > LETTERS.length() - 1) currentIndex = LETTERS.length();
		
		setSelectedIndex(currentIndex);
		if(letterChangeListener != null) {
			letterChangeListener.onLetterChange(currentIndex);
		}
		
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			setBackgroundColor(colorTouched);
			break;
		case MotionEvent.ACTION_UP:
			setBackgroundColor(colorUntouched);
		}
		
		return true;
	}
	
	public void setOnLetterChangeListener(OnLetterChangeListener letterChangeListener){
		this.letterChangeListener = letterChangeListener;
	}
	
	public void setSelectedIndex(int selectedIndex) {
		currentIndex = selectedIndex;
		invalidate();
	}

	// *********************************
	// getters and setters
	// *********************************
	public interface OnLetterChangeListener{
		void onLetterChange(int selectedIndex);
	}
	
	public int getColorTouched() {
		return colorTouched;
	}

	public void setColorTouched(int colorTouched) {
		this.colorTouched = colorTouched;
	}

	public int getColorUntouched() {
		return colorUntouched;
	}

	public void setColorUntouched(int colorUntouched) {
		this.colorUntouched = colorUntouched;
	}

	public int getColorSelected() {
		return colorSelected;
	}

	public void setColorSelected(int colorSelected) {
		this.colorSelected = colorSelected;
	}

	public int getColorUnSelected() {
		return colorUnSelected;
	}

	public void setColorUnSelected(int colorUnSelected) {
		this.colorUnSelected = colorUnSelected;
	}

	public int getTextSize() {
		return textSize;
	}

	public void setTextSize(int textSize) {
		this.textSize = textSize;
	}
}
